package cc.sheji.controller;

import cc.sheji.Service.CategoryService;
import cc.sheji.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;

@Validated
@RestController

@RequestMapping(value = "/api/v1/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @RequestMapping("/{id}")
    public JsonResult get(@PathVariable("id") Long id) throws Exception {
        return JsonResult.Success(categoryService.get(id));
    }



    @PostMapping()
    public JsonResult create(@RequestBody @Validated Category entity) {
        entity.setId(null);
        return JsonResult.Success(categoryService.create(entity));
    }

    @PutMapping("/{id}")
    public JsonResult update(@PathVariable(name = "id") Long id, @RequestBody Category entity) {
        entity.setId(id);
        return JsonResult.Success(categoryService.update(entity));
    }

    @GetMapping()
    public JsonResult get(CategoryQueryCriteria criteria , Pageable pageable) throws InterruptedException {
        return JsonResult.Success(categoryService.list(criteria,pageable));
    }

    @GetMapping("/all")
    public JsonResult getAll(CategoryQueryCriteria criteria ) throws InterruptedException {
        return JsonResult.Success(categoryService.getAll(criteria));
    }

}
